package kr.nexmore.slmsample2.view;

import java.io.IOException;
import java.net.URL;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import kr.nexmore.slmsample2.uri.Part_Uri;

import org.apache.log4j.PropertyConfigurator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.services.internal.events.EventBroker;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.swt.modeling.EMenuService;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Text;

@SuppressWarnings("restriction")
public class LoginFrm extends SWT implements Listener {
	private Text id_txt;
	private Text pw_txt;

	private Composite c_parent;

	@Inject
	private MWindow winodw;

	@Inject
	private MApplication application;

	@Inject
	private EPartService ePartService;

	/*
	 * @Inject private UISynchronize ui;// setEnable메소드는 UI쓰레드에서 호출해야한다.
	 */
	@Inject
	private EventBroker broker;

	public LoginFrm() {

		// File f = new File("log4j.properties");
		// PropertyConfigurator.configure("log4j.properties");
		try {
			URL installURL = Platform.getBundle("kr.nexmore.slmsample2")
					.getEntry("/");
			String installPath = Platform.asLocalURL(installURL).getFile();
			PropertyConfigurator.configure(installPath + "/log4j.properties");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * Create contents of the view part.
	 */
	@PostConstruct
	
	
	public void createControls(final Composite parent) {
		parent.setLayout(new FormLayout());

		c_parent = parent;

		c_parent.addListener(SWT.CLOSE, this);
		Label lblSlmLogin = new Label(parent, SWT.NONE);
		FormData fd_lblSlmLogin = new FormData();
		fd_lblSlmLogin.left = new FormAttachment(0, 10);
		lblSlmLogin.setLayoutData(fd_lblSlmLogin);
		lblSlmLogin.setText("SLM Login");

		id_txt = new Text(parent, SWT.BORDER);
		fd_lblSlmLogin.bottom = new FormAttachment(id_txt, -6);
		id_txt.setText("aa");
		FormData fd_id_txt = new FormData();
		fd_id_txt.left = new FormAttachment(0, 43);
		fd_id_txt.right = new FormAttachment(0, 160);
		id_txt.setLayoutData(fd_id_txt);

		pw_txt = new Text(parent, SWT.BORDER | SWT.PASSWORD);
		pw_txt.setText("bb");
		fd_id_txt.bottom = new FormAttachment(100, -235);
		FormData fd_pw_txt = new FormData();
		fd_pw_txt.right = new FormAttachment(id_txt, 0, SWT.RIGHT);
		fd_pw_txt.top = new FormAttachment(id_txt, 8);
		fd_pw_txt.left = new FormAttachment(id_txt, 0, SWT.LEFT);
		pw_txt.setLayoutData(fd_pw_txt);

		final Button btnNewButton = new Button(parent, SWT.NONE);

		btnNewButton.setSelection(true);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				if (id_txt.getText().equals("aa")
						&& pw_txt.getText().equals("bb")) {// 로그인성공시
					winodw.setVisible(false);
					/*
					 * MainFrm frm = new MainFrm();
					 * 
					 * frm.open();// 메인 띄우기
					 */

					broker.post("hylist/ani", id_txt.getText());

					application.getChildren().get(1).setVisible(true);
				} else {
					MessageDialog dialog = new MessageDialog(e.display
							.getActiveShell(), "아이디&비밀번호 오류", null,
							"아이디 또는 비번이 틀렷음", MessageDialog.ERROR,
							new String[] { "확인" }, 0);
					dialog.open(); // 실패 창 열기

				}

			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.top = new FormAttachment(pw_txt, -51);
		fd_btnNewButton.bottom = new FormAttachment(pw_txt, 0, SWT.BOTTOM);
		fd_btnNewButton.left = new FormAttachment(id_txt, 36);
		fd_btnNewButton.right = new FormAttachment(100, -147);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\uB85C\uADF8\uC778");

		Monitor primaryMonitor = parent.getDisplay().getPrimaryMonitor();
		Rectangle bounds = primaryMonitor.getBounds();
		Rectangle rect = parent.getShell().getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		parent.getShell().setLocation(x, y);

	}

	@PreDestroy
	public void dispose() {
	}

	@Focus
	public void setFocus() {
		// TODO Set the focus to control
	}

	public void create(Composite parent, EMenuService menu) {

		menu.registerContextMenu(parent, Part_Uri.WINDOW_MAIN);
	}

	@Override
	public void handleEvent(Event event) {
		System.out.println("까꿍");
		// TODO Auto-generated method stub

	}

	/*
	 * @Inject
	 * 
	 * @Optional public void receiveEvent(@EventTopic("hylist/ani") String data)
	 * {
	 * 
	 * System.out.println("main2receiveEvent:"+data);
	 * //lblNewLabel.setText(data);
	 * 
	 * }
	 */
}
